let search_value = '';
let isMyCompany_value = 0;  // 是否是本公司岗位
let position_value = "";    //  工作地点
let minSalary_value = "";      //  最低工资
let maxSalary_value = "";      //  最高工资
let salary_util = "";         //  工资单位
let experience_value = "";    //  工作经验
let education_value = "";      //  学历要求

// 网关配置的前缀
var jobsServer = "/jobsSer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量

let companyId = "";
let bookmarkList = [];

// let userId = "1354255569179971599";
let userId = "";

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").html();
    let tmp = $("#searchValue").val();
    if (tmp === "" ||
        tmp === null ||
        tmp === "-"){
        tmp = "";
    }
    search_value = tmp;
    $('#search').val(search_value)

    getBookmarkList();
    getSalaryUnit();
    getExperience();
    getEducation();
    getJobList(currentPage, pageSize, search_value,
        minSalary_value, maxSalary_value, salary_util,
        experience_value, education_value, companyId)
}

function getJobList(currentPage, pageSize, search_value,
                    minSalary_value, maxSalary_value, salary_util,
                    experience_value, education_value, companyId) {

    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/jobInfo",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            salaryMin: minSalary_value,
            salaryMax: maxSalary_value,
            salaryUnit: salary_util,
            experience: experience_value,
            education: education_value,
            deleted: 0,
            searchValue: search_value,
            companyId: companyId,
            status: "正常"
        },
        success: function (data) {
            console.log("所有的岗位数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            $("#total").html(total);
            //清空数据
            $("#jobsList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                let jobList = ["", "", "", ""];
                let temp = [];
                if (item.skills !== "" && item.skills !== null) {
                    temp = item.skills.split(",");
                    $.each(temp, function (i, skill) {
                        jobList[i] = skill;
                    })
                }

                let id = item.jobId;
                let aixin = "";
                if ($.inArray(id, bookmarkList) >= 0) {
                    aixin = "<img src='/images/aixin-2.png' style='width: 20px;height: 20px;' />";
                } else {
                    aixin = "<i class='far fa-heart'></i>";
                }

                $("#jobsList").append(
                    "<div class='col-lg-12 mb-20'>" +
                    "<div class='single-job style-two'>" +
                    "<div class='info-top'>" +
                    "<div class='job-info'>" +
                    "<div class='job-info-inner'>" +
                    "<div class='job-info-top'>" +
                    "<div class='saveJob for-listing'>" +
                    "<span class='featured-label'>精选</span>" +
                    "<a class='job-type-label ml-20 mr-20'>" + item.jobType + "</a>" +
                    "<a class='save-job' data-toggle='modal' disabled='disabled'>" +
                    aixin +
                    "</a>" +
                    "</div>" +
                    "<div class='title-name'>" +
                    "<h3 class='job-title'>" +
                    "<a onclick='jobDetail(\""+item.jobId+"\", \""+item.companyId+"\")'>" + item.jobName + "</a>" +
                    "</h3>" +
                    "<div class='employer-name'>" +
                    "<a onclick='companyDetail(\"" + item.companyId + "\")'>" + item.companyName + "</a>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "<div class='job-meta-two'>" +
                    "<div class='field-salary_from'>" +
                    "￥&nbsp;" + item.salaryMin + "&nbsp;-&nbsp;" + item.salaryMax + "&nbsp;" + item.salaryUnit +
                    "</div>" +
                    "<div class='field-datetime'>" +
                    "<i class='lnr lnr-clock'></i>" + item.updateDate + "" +
                    "</div>" +
                    "</div>" +
                    "<div class=job-skill-tag>" +
                    "<a>" + jobList[0] + "</a>" +
                    "<a>" + jobList[1] + "</a>" +
                    "<a>" + jobList[2] + "</a>" +
                    "<a>" + jobList[3] + "</a>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>"
                );

            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getJobList(num, size, search_value,
                        minSalary_value, maxSalary_value, salary_util,
                        experience_value, education_value, companyId)
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

function getBookmarkList() {
    $.ajax({
        type: "GET",
        async: false,
        url: jobsServer + "/jobs/getBookmarks?userId=" + userId,
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            bookmarkList = data.datas;
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

// 获得 工资单位
function getSalaryUnit() {
    let $list = $("#salary_util .list");
    let $mySelect = $("#salary_util .mySelect");
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/salaryUnit",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("工资单位");

            // 如果为空
            if (data.datas.length <= 0) {
                $mySelect.empty();
                $mySelect.append(
                    "<option>暂无数据</option>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                if (item.value === "千/月") {
                    $('#salary_util .current').html("千/月");
                }
                $list.append(
                    "<li data-value='" + item.value + "' class='option'>" + item.value + "</li>"
                );
            });
        },
        error: function (err) {
            $mySelect.empty();
            $mySelect.append(
                "<option>暂无数据</option>"
            );
            console.log(err);
        }

    });
}

// 获得 工作经验
function getExperience() {
    let $experienceList = $("#experienceList");
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/experience",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("工作经验");

            // 如果为空
            if (data.datas.length <= 0) {
                $experienceList.empty();
                $experienceList.append(
                    "<li>" +
                    "<div class='filter-name-item'>" +
                    "<label> 暂无数据 </label>" +
                    "</div>" +
                    "</li>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                $experienceList.append(
                    "<li>" +
                    " <div class='filter-name-item'>" +
                    "<input value='" + item.value + "' type='radio' name='experience' >" +
                    "<label> " + item.value + " </label>" +
                    "</div>" +
                    "</li>"
                );
            });
        },
        error: function (err) {
            $experienceList.empty();
            $experienceList.append(
                "<li>" +
                "<div class='filter-name-item'>" +
                "<label> 暂无数据 </label>" +
                "</div>" +
                "</li>"
            );
        }

    });
}

// 获得 学历要求
function getEducation() {
    let $educationList = $("#educationList");
    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/educationLevel",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("学历要求");

            // 如果为空
            if (data.datas.length <= 0) {
                $educationList.empty();
                $educationList.append(
                    "<li>" +
                    "<div class='filter-name-item'>" +
                    "<label> 暂无数据 </label>" +
                    "</div>" +
                    "</li>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                $educationList.append(
                    "<li>" +
                    "<div class='filter-name-item'>" +
                    "<input type='radio' value='" + item.value + "' name='education'>" +
                    "<label> " + item.value + " </label>" +
                    "</div>" +
                    "</li>"
                );
            });
        },
        error: function (err) {
            $educationList.empty();
            $educationList.append(
                "<li>" +
                "<div class='filter-name-item'>" +
                "<label> 暂无数据 </label>" +
                "</div>" +
                "</li>"
            );
        }

    });
}

// 搜索框
function click_search() {
    let tmp = $('#search').val();
    if (tmp === "" || tmp === null) {
        tmp = "";
    }
    search_value = tmp;
    ArgsChanged();
}

// 是否是本公司的岗位
function click_isMyCompany() {
    var radio = document.getElementsByName("isMyCompany");
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked == true) {
            isMyCompany_value = radio[i].value;
            break;
        }
    }
    if (isMyCompany_value === 0) {
        companyId = "";
    } else {
        companyId = "400001";
    }
    ArgsChanged();
}

//  工作地点
function click_position() {
    position_value = $('#position').val();
    ArgsChanged();
}

//  最低工资
function click_minSalary() {
    var tmp = $('#salary-min').val();

    if (!salary_error(tmp)) {
        $('#salary-min').val('');
        return;
    }
    if (tmp < 0) {
        $('#salary-error').append('请输入大于0的数');
    }
    if (maxSalary_value != 0 && maxSalary_value <= tmp) {
        $('#salary-error').append('请输入小于最大值的数');
        $('#salary-min').val('');
        return;
    }

    minSalary_value = tmp;
    click_salaryUtil()
    ArgsChanged();
}

//  最高工资
function click_maxSalary() {
    var tmp = $('#salary-max').val();

    if (!salary_error(tmp)) {
        $('#salary-max').val('');
        return;
    }
    if (minSalary_value != 0 && minSalary_value >= tmp) {
        $('#salary-error').append('请输入大于最小值的数');
        $('#salary-max').val('');
        return;
    }

    maxSalary_value = tmp;
    click_salaryUtil()
    ArgsChanged();
}

// 工资单位
function click_salaryUtil() {
    if ((minSalary_value === null || minSalary_value === "") &&
        (maxSalary_value === null || maxSalary_value === "")) {
        salary_util = "";
    } else {
        salary_util = $('#salary_util .current').html();
    }
    ArgsChanged();
}

// 清除输入的工资
function salary_clean() {
    $('#salary-error').html('');
    $('#salary-min').val('');
    minSalary_value = "";
    $('#salary-max').val('');
    maxSalary_value = "";
    salary_util = "";
    ArgsChanged();
}

//  工作经验
function click_experience() {
    var radio = document.getElementsByName("experience");
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked == true) {
            experience_value = radio[i].value;
            break;
        }
    }
    ArgsChanged();
}

//  学历要求
function click_education() {
    var radio = document.getElementsByName("education");
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked == true) {
            education_value = radio[i].value;
            break;
        }
    }
    ArgsChanged();
}

function ArgsChanged() {
    getJobList(1, pageSize, search_value,
        minSalary_value, maxSalary_value, salary_util,
        experience_value, education_value, companyId);
}

function salary_error(tmp) {
    $('#salary-error').html('');
    var reg = /^[0-9]+.?[0-9]*$/;
    var pattern = new RegExp(reg);
    if (!pattern.test(tmp)) {
        $('#salary-error').append('请输入数值类型');
        return false;
    } else {
        return true;
    }
}

function jobDetail(jobId, companyId){
    location.href ="/oa/jobs/jobDetails/"+jobId+"/"+companyId;
}
function companyDetail(companyId){
    location.href ="/oa/company/companyDetailsPage/"+companyId;
}
